package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.cookbook.dao.AttentionDAO;
import com.wanmait.cookbook.dao.UserinfoDAO;
import com.wanmait.cookbook.vo.Pager;
import com.wanmait.cookbook.vo.Userinfo;

/**
 * Servlet implementation class HotUserinfoServlet
 */
@WebServlet("/hotUserinfoServlet")
public class HotUserinfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HotUserinfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action=request.getParameter("action");
		if("hotUserinfoShowList".equals(action)) {
			this.hotUserinfoShowList(request, response);
		}
		else if("pagerHotUserinfo".equals(action)) {
			this.pagerHotUserinfo(request, response);
		}
	}
	//ajax ������֣���Ӧajaxִ�и÷���
	public void pagerHotUserinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String pageNo=request.getParameter("pageNo");
		Pager pager=new Pager();
		pager.setPageSize(2);
		int rows=UserinfoDAO.getUserinfoDAO().getHotUserinfoRows();
		pager.setPageCount(rows);
		if(Integer.parseInt(pageNo)<1) {
			pageNo=1+"";
		}
		if(Integer.parseInt(pageNo)>pager.getPageCount()) {
			pageNo=pager.getPageCount()+"";
		}
		pager.setPageNo(Integer.parseInt(pageNo));
		List<Userinfo> allHotUserinfo=(List<Userinfo>)UserinfoDAO.getUserinfoDAO().allHotUserinfo(pager);
		List<Integer> attentionCounts=new ArrayList<Integer>();
		for(Userinfo everyHotUserinfo:allHotUserinfo) {
			int attentionCount=AttentionDAO.getAttentionDAO().attentionCount(everyHotUserinfo);
			attentionCounts.add(attentionCount);
		}
		request.setAttribute("allHotUserinfo", allHotUserinfo);
		request.setAttribute("attentionCounts", attentionCounts);
		request.setAttribute("pager", pager);
		
		
		request.getRequestDispatcher("/inc/ajaxInc/hotUserinfoInc.jsp").forward(request, response);
		
	}
	//չʾ�������Ŵ��˵���Ϣ
	public void hotUserinfoShowList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Pager pager=new Pager();
		pager.setPageSize(2);//��ÿҳ��ʾ����
		int rows=UserinfoDAO.getUserinfoDAO().getHotUserinfoRows();//�ܹ��ж�����/���ж�����������
		pager.setPageCount(rows);//�����ܹ��ж���ҳ
		pager.setPageNo(1);
		
		List<Userinfo> allHotUserinfo=(List<Userinfo>)UserinfoDAO.getUserinfoDAO().allHotUserinfo(pager);
		List<Integer> attentionCounts=new ArrayList<Integer>();
		for(Userinfo everyHotUserinfo:allHotUserinfo) {
			int attentionCount=AttentionDAO.getAttentionDAO().attentionCount(everyHotUserinfo);
			attentionCounts.add(attentionCount);
		}	
		request.setAttribute("allHotUserinfo", allHotUserinfo);
		request.setAttribute("attentionCounts", attentionCounts);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/hotUserinfoShowList.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
